DAY39:Smallest possible sum


Posted by birdbirdmurmur on 2023-08-21

題目連結

https://www.codewars.com/kata/52f677797c461daaf7000740

解法

function solution(X) {

    function gcd(a, b) {
        if (b === 0) {
            return a;
        }
        return gcd(b, a % b);
    }

    const n = X.length;
    let result = X[0];

    for (let i = 1; i < n; i++) {
        result = gcd(result, X[i]);
    }

    return result * n;
}

筆記


#javascript #Codewars #gcd







Related Posts

關於 React 小書:PropTypes 和 Component 參數驗證

關於 React 小書:PropTypes 和 Component 參數驗證

細節-型別轉換和陣列篩選

細節-型別轉換和陣列篩選

讀書筆記-版本控制使用Git: 檔案管理、索引、送交

讀書筆記-版本控制使用Git: 檔案管理、索引、送交


Comments